﻿using Spectre.Console;
using System.Text;

namespace gpt
{
    /// <summary>
    /// 配色方案
    /// </summary>
    public static class ConsoleColor
    {
        public static string System = "cyan1";
        public static string Content = "steelblue1";
        public static string Error = "red";
        public static string Highlight = "lime";
        public static string ChatContent = "paleturquoise1";
    }

    public static class ConsoleTools
    {
        public static string ReadMultilineInput()
        {
            AnsiConsole.Write(new Markup("[bold cyan]>>> [/]"));
            var str = Console.ReadLine();
            if (string.IsNullOrWhiteSpace(str)) return str;

            if (str.EndsWith("\\")) return str.Remove(str.Length - 1, 1) + Environment.NewLine + ReadMultilineInput();
            return str;
        }
  

        public static void SystemSay(string message)
        {
            AnsiConsole.Write(new Markup($"[{ConsoleColor.System}][[系统]][/] {message.Replace("[", "[[").Replace("]", "]]")}"));
            Console.WriteLine();
        }

        public static void SystemSayNoFormat(string message)
        {
            try
            {
                AnsiConsole.Write(new Markup($"[{ConsoleColor.System}][[系统]][/] {message}"));
                Console.WriteLine();
            }catch(Exception)
            {
                SystemSay(message);
            }
            
        }

        public static void ErrorOut(string message)
        {
            AnsiConsole.Write(new Markup($"[{ConsoleColor.Error}][[错误]][/] {message.Replace("[", "[[").Replace("]", "]]")}"));
            Console.WriteLine();
        }

        public static void ErrorOutNoFormat(string message)
        {
            try
            {
                AnsiConsole.Write(new Markup($"[{ConsoleColor.Error}][[错误]][/] {message}"));
                Console.WriteLine();
            }
            catch (Exception)
            {
                ErrorOut(message);
            }
       
        }

        public static void HelloMenu(GptModel model)
        {
            StringBuilder sr = new StringBuilder();

            sr.AppendLine($"[bold yellow]{AICMD.Logo.Replace("[", "[[").Replace("]", "]]")}[/]");
            sr.AppendLine("NOW：" + DateTime.Now.ToLongTimeString());
            sr.AppendLine($"使用[{ConsoleColor.Content}]`{AICMD.Prefix}`[/]开头执行命令");
            sr.AppendLine($"使用[{ConsoleColor.Content}]`{AICMD.Help}`[/]查看帮助");
            sr.AppendLine($"当前模型：[{ConsoleColor.Highlight}]{model.Model}[/]");

            var panel = new Panel(sr.ToString());
            panel.Border = BoxBorder.Heavy;
            panel.Header = new PanelHeader($"[bold {ConsoleColor.System}]Console.AI[/]");

            AnsiConsole.Write(panel);
        }

        static string[] CmdList1 = new[] {
            "1.当前信息", "2.常看快捷命令", "3.开始新对话",
            "4.切换模型", "5.添加模型", "6.模型列表",
            "7.返回对话","8.将当前模型设为默认", "9.打开配置文件","10.退出系统"
        };

        public static int CMDSelect()
        {
            var fruit = AnsiConsole.Prompt(
                new SelectionPrompt<string>()
                .Title($"[{ConsoleColor.System}]命令列表[/]")
                .PageSize(10)
                .MoreChoicesText($"[{ConsoleColor.System}](上下移动光标以展示很多选择)[/]")
                .AddChoices(CmdList1));
            return CmdList1.ToList().IndexOf(fruit);
        }

    }
}
